import pynmea2
import time

def gps_nmea():
    count = 0
    while (count<5):
        # 读取一行gps信息
        # line = ser.readline()
        fw = open('g.txt', "r+", encoding='utf-8', errors='ignore')
        # line = str(str(ser.readline())[2:])  # 将读取到的字节码转化成字符串（去掉前2位的无用字符）
        for line in fw:
            # 寻找有地理坐标的那一行数据
            if line.startswith('$GPGGA'):
                print("*********************")
                line = line.replace('\\r\\n\'', '')  # 字符串结尾的无用换行符
                print(line)
                record = pynmea2.parse(line)
                print("UTC:",  record.timestamp)
                print("LAT:", record.latitude, "度/N")
                print(record.longitude, "度/E")
                print('卫星个数:',record.num_sats)
                count = count + 1
            if line.startswith('$GPGSV'):
                print("*********************")
                line = line.replace('\\r\\n\'', '')  # 字符串结尾的无用换行符
                print(line)
                record = pynmea2.parse(line)
                print('Massege1:')
                print('PRN:', record.sv_prn_num_1)
                print('仰角:', record.elevation_deg_1)
                print('方位角:', record.azimuth_1)
                if record.azimuth_1 != 0:
                    print('Massege2:')
                    print('PRN:', record.sv_prn_num_2)
                    print('仰角:', record.elevation_deg_2)
                    print('方位角:', record.azimuth_2)
                if record.azimuth_1 != 0:
                    print('Massege3:')
                    print('PRN:', record.sv_prn_num_3)
                    print('仰角:', record.elevation_deg_3)
                    print('方位角:', record.azimuth_3)
                count = count + 1
                print("************")
            if (count == 100):
                break
            # if line.startswith('$GPGSV'):
            #     line = line.replace('\\r\\n\'', '')  # 字符串结尾的无用换行符
            #     print(line)
            #     rmc = pynmea2.parse(line)
            #     print('UTC:', rmc.timestamp)
            #     print("LAT:", rmc.latitude, "度/N", " ", rmc.longitude, "度/E")
            #     print('卫星个数:', rmc.num_sats)
            #     count = count + 1
